<?php

declare(strict_types=1);

namespace app\shop\controller\api;

use think\Request;
use sheep\controller\traits\Util;
use app\shop\model\decorate\Decorate;
use app\shop\model\decorate\Page;
use sheep\library\easywechatPlus\WechatMiniProgramShop;

class Index extends ShopBase
{
    use Util;

    public function init()
    {
        $platform = $this->request->header('platform');
        $templateId = $this->request->param('templateId', 0);
        $platformConfig = sheep_config("shop.platform.$platform");

        if (empty($platformConfig['status']) || !$platformConfig['status']) {
            return error('暂不支持该平台,请前往商城配置启用对应平台');
        }

        $template = Decorate::template()->whereRaw("find_in_set('$platform', platform)");

        if ($templateId) {
            $template->where('id', $templateId);
        } else {
            $template->where('status', 'enable');
        }
        $template = $template->find();
        if ($template) {
            $template = Page::where('decorate_id', $template->id)->select()->column('page', 'type');
        }

        $shopConfig = sheep_config('shop.basic');

        // 客服配置
        $chatSystem = config('chat.system');
        // 客服应用配置
        $chatConfig = config('chat.application.shop');
        // 初始化 socket ssl 类型, 默认 none
        $ssl = $chatSystem['ssl'] ?? 'none';
        $chat_domain = ($ssl == 'none' ? 'http://' : 'https://') . request()->host(true) . ($ssl == 'reverse_proxy' ? '' : (':' . $chatSystem['port'])) . '/chat';
        $chatConfig['chat_domain'] = $chat_domain;

        $data = [
            'app' => [
                'name' => $shopConfig['name'],
                'logo' => $shopConfig['logo'],
                'cdnurl' => domainurl('', true),
                'filesystem' => config('filesystem.default'),
                'version' => $shopConfig['version'],
                'user_protocol' => $shopConfig['user_protocol'],
                'privacy_protocol' =>  $shopConfig['privacy_protocol'],
                'about_us' => $shopConfig['about_us'],
                'copyright' => $shopConfig['copyright'],
                'copytime' => $shopConfig['copytime'],
            ],
            'platform' => [
                'auto_login' => $platformConfig['auto_login'] ?? 0,
                'bind_mobile' => $platformConfig['bind_mobile'] ?? 0,
                'payment' => $platformConfig['payment']['methods'],
                'recharge_payment' => sheep_config('shop.recharge_withdraw.recharge.methods'),      // 充值支持的支付方式
                'share' => $platformConfig['share'],
            ],
            'template' => $template,
            'chat' => $chatConfig
        ];

        if ($platform == 'WechatMiniProgram') {
            $uploadshoppingInfo = new WechatMiniProgramShop(app('shop.wechat.mini_program'));
            $data['has_wechat_trade_managed'] = intval($uploadshoppingInfo->isTradeManaged());
        }
        return success('初始化', $data);
    }

    public function pageSync()
    {
        $pages = $this->request->post('pages');
        foreach ($pages as $page) {
            if (!empty($page['meta']['sync']) && $page['meta']['sync']) {
                $data = \app\shop\model\data\Page::getByPath($page['path']);
                $name = $page['meta']['title'] ?? '未命名';
                $group = $page['meta']['group'] ?? '其它';
                if ($data) {
                    $data->name = $name;
                    $data->group = $group;
                    $data->save();
                } else {
                    \app\shop\model\data\Page::create([
                        'name' => $name,
                        'group' => $group,
                        'path' => $page['path']
                    ]);
                }
            }
        }
        return success();
    }

    public function page($id)
    {
        $template = \app\shop\model\decorate\Decorate::typeDiypage()->with('diypage')->findOrFail($id);
        return success('', $template);
    }

    public function feedback(Request $request)
    {
        $user = $this->auth()->user();
        $params = $request->only(['type', 'content', 'images', 'phone']);
        if ($user) {
            $params['user_id'] = $user->id;
        }
        $result = \app\shop\model\Feedback::create($params);
        if ($result) {
            return success('感谢您的反馈');
        }
    }
}
